package com.fs.iot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fs.common.base.service.AbstractBaseEntityService;
import com.fs.common.exception.AppException;
import com.fs.iot.bean.dto.DeviceModelFieldDTO;
import com.fs.iot.bean.query.DeviceModelFieldQuery;
import com.fs.iot.domain.entity.DeviceModelFieldEntity;
import com.fs.iot.domain.mapper.DeviceModelFieldMapper;
import com.fs.iot.service.DeviceModelFieldEntityService;
import com.fs.common.ErrorCodes;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 模型字段服务实现 
 * @author Coder Generator 2025-03-22 15:13:52 
 **/
@Service
public class DeviceModelFieldEntityServiceImpl extends AbstractBaseEntityService<DeviceModelFieldEntity, DeviceModelFieldDTO, DeviceModelFieldMapper, DeviceModelFieldQuery> implements DeviceModelFieldEntityService {
    @Override
    public DeviceModelFieldDTO toDTO(DeviceModelFieldEntity entity) {
        DeviceModelFieldDTO dto = new DeviceModelFieldDTO();
        BeanUtils.copyProperties(entity, dto);
        return dto;
    }

    @Override
    public DeviceModelFieldEntity toEntity(DeviceModelFieldDTO dto) {
        DeviceModelFieldEntity entity = new DeviceModelFieldEntity();
        BeanUtils.copyProperties(dto, entity);
        return entity;
    }

    @Override
    protected QueryWrapper<DeviceModelFieldEntity> queryToWrapper(DeviceModelFieldQuery query) {
        return this.createQueryWrapper()
                .eq(StringUtils.isNotBlank(query.getId()), "id", query.getId())
                .in(null != query.getIds(), "id", query.getIds())
                .eq(StringUtils.isNotBlank(query.getModelId()), "model_id", query.getModelId())
                .eq(StringUtils.isNotBlank(query.getCode()), "code", query.getCode())
                .eq(StringUtils.isNotBlank(query.getName()), "name", query.getName())
                .orderByDesc("create_time");
    }

    /**
     * 插入前处理
     * 可以执行相关参数校验，校验不通过时抛出异常
     *
     * @return false时不进行插入，true时进行插入
     */
    @Override
    protected boolean processBeforeInsert(DeviceModelFieldDTO dto) {
        // 检查设备编号是否重复
        String code = dto.getCode();
        String modelId = dto.getModelId();
        List<DeviceModelFieldDTO> list = this.queryBuilder()
                .eq("code", code).eq("model_id", modelId)
                .query();
        if (!list.isEmpty()) {
            throw AppException.of(ErrorCodes.IOT_MODEL_FIELD_CODE_DUPLICATED);
        }

        return super.processBeforeInsert(dto);
    }

    /**
     * 更新前处理
     *
     * @return false时不再进行更新，true时进行更新
     */
    @Override
    protected boolean processBeforeUpdate(DeviceModelFieldDTO dto) {
        String code = dto.getCode();
        if (StringUtils.isNotBlank(code)) {
            List<DeviceModelFieldDTO> list = this.queryBuilder()
                    .eq("code", code)
                    .neq("id", dto.getId())
                    .eq("model_id", dto.getModelId())
                    .query();
            if (!list.isEmpty()) {
                throw AppException.of(ErrorCodes.IOT_DEVICE_CODE_DUPLICATED);
            }
        }

        return super.processBeforeUpdate(dto);
    }

    /**
     * 根据模型删除记录
     *
     * @param modelId 模型id
     */
    @Override
    public void deleteByModel(String modelId) {
        QueryWrapper<DeviceModelFieldEntity> queryWrapper = this.createQueryWrapper();
        queryWrapper.eq("model_id", modelId);
        this.remove(queryWrapper);
    }
}